package moxl.parser.assemblers.pi;

import moxl.parser.ParserException;
import moxl.parser.assemblers.AbstractTokenFedMarkupAssembler;
import moxl.parser.assemblers.AssemblerException;
import moxl.tokenizer.TokenStream;
import moxl.tokenizer.TokenizerException;
import moxl.tokenizer.xml.XmlTokenType;

public class ProcessingInstructionAssembler extends AbstractTokenFedMarkupAssembler<ProcessingInstructionAssemblerState, ProcessingInstructionAssemblerContext>
{
	@Override
	protected ProcessingInstructionAssemblerContext createAssemblerContext(TokenStream tstream) throws ParserException, TokenizerException
	{
		return new ProcessingInstructionAssemblerContext(tstream);
	}

	@Override
	protected void handleInput(XmlTokenType type, ProcessingInstructionAssemblerContext ctx) throws ParserException, TokenizerException
	{
		ProcessingInstructionStateDelegateBase delegate = ctx.getState().getDelegate();
		switch (type)
		{
		case OPEN_CHEVRON_QUESTION_MARK:
			delegate.acceptOpenChevronQuestionMark(ctx);
			break;

		case NAME:
			delegate.acceptName(ctx);
			break;
			
		case PROCESSING_INSTRUCTION_BODY:
			delegate.acceptProcessingInstructionBody(ctx);
			break;
			
		default:
			// TODO: Review the use of these exceptions, might be more informational exceptions possible that include the current state.
			throw new AssemblerException("Parser cannot handle token: " + ctx.getToken(), ctx.getLocation());
		}
	}
}
